<template>
	<view class="">
		<!-- 招标信息 -->
		<view class="active-list">
			<view class="active-title">
				<view class="active-top">
					<text class="">*</text>产品名称<text class="tip" v-if="blurProNullShows">请填写产品名称</text>
				</view>
				<u--textarea placeholder="请填写产品名称(必填)" @blur="proBlurs(form.name)" v-model="form.name" border="none"
					maxlength="30"></u--textarea>
			</view>
			<view class="active-title">
				<view class="active-top">
					<text class="">*</text>产品价格<text class="tip" v-if="blurProNumNullShow">请填写价格</text>
				</view>
				<!-- <u--textarea :formatter="numFormatter" placeholder="请输入联系电话(必填)" @blur="phoneBlur(phone)"
					v-model="phone" border="none" maxlength="11"></u--textarea> -->
				<u--textarea :formatter="numFormatter" placeholder="请填写产品规格及单位(必填)" @blur="proNumBlurs(form.price)"
					v-model="form.price" border="none" maxlength="10"></u--textarea>
			</view>
			<view class="active-title">
				<view class="active-top">
					<text class="">*</text>产品规格<text class="tip" v-if="blurProSpecsNullShow">请填写产品规格</text>
				</view>
				<u--textarea placeholder="请填写产品规格(必填)" @blur="proSpecsBlurs(form.specs)" v-model="form.specs"
					border="none" maxlength="10"></u--textarea>
			</view>
			<view class="active-title">
				<view class="active-top">
					产品库存
				</view>
				<view class="num-box">
					<u-number-box v-model="form.stock" :min="1" :max="999999" integer
						inputWidth="130rpx"></u-number-box>
				</view>
			</view>


			<!-- <view class="active-title">
				<view class="active-top">
					产品属性
				</view>
				<u--textarea placeholder="点击添加自定义产品详细属性" @focus="productAttributeFocus" v-model="productAttribute" border="none"
					></u--textarea>

			</view>
			 -->

			<view class="active-title describe-title">
				<view class="active-top">
					产品描述
				</view>
				<u--textarea placeholder="提供更多产品细节,帮助采购了解你的产品" v-model="form.perfile" border="none"
					maxlength="100"></u--textarea>
			</view>

			<view class="active-title active-info">
				<view class="active-top">
					<text class="tip">*</text>产品宣传图({{fileList1.length}}/5)
				</view>
				<view class="active-img-des">
					(第一张产品主图)
				</view>
				<view class="active-info-img">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="5" width="204rpx" height="204rpx">
					</u-upload>
				</view>
			</view>


			<view class="sign-up">
				<u-button type="primary" color="#2684ff" text="保存" :throttleTime="500" :loading="submitLoad"
					@click="nextStep">
				</u-button>
			</view>

		</view>
	</view>
</template>

<script>
	import baseConfig from '@/config/baseUrl.js'
	import {
		mapState,
		mapGetters
	} from 'vuex'
	export default {
		name: "supplyProduct",
		data() {
			return {
				form: {
					name: '', //产品名称输入信息
					price: '', //产品价格输入信息
					specs: '', //产品规格
					stock: '1', //产品库存
					perfile: '', //产品描述
				},
				blurProNullShows: false, //产品名称信息为空失焦显示
				blurProNumNullShow: false, //产品价格输入信息为空失焦显示
				blurProNameNullShow: false, //公司名称输入信息为空失焦显示
				blurProSpecsNullShow: false, //产品规格失焦
				submitLoad: false, //提交加载
				productAttribute: '', //产品属性
				fileList1: [], //产品图
				imageUrl: '',
				imgArr: [],
				supplierId: '',
			}
		},
		onLoad(option) {
			let openId = uni.getStorageSync('openId')
			this.openId = openId.openid
			this.supplierId = option.supplierId
			if (option.submitInfo) this.setForm(option.submitInfo)
		},
		methods: {
			// 数量输入框限制输入字数
			numFormatter(value) {
				let str = value.replace(/[^\d]/g, "")
				if (str == '') {
					str = ''
				} else if (Number(str) === 0) {
					str = 0
				} else {
					str = Number(str)
				}

				return str
			},
			//设置数据回显
			//perfile 空值报错 图片回显
			setForm(data) {
				this.form = JSON.parse(data) || {}
				this.form.perfile = this.form.perfile || ''
				let arr = this.form.imageList ? this.form.imageList.split(',') : []
				this.fileList1 = arr.map(i => {
					return {
						url: i
					}
				})
				uni.setNavigationBarTitle({
					title: '编辑供应'
				})
			},
			// 产品名称失焦
			proBlurs(e) {
				if (e) {
					this.blurProNullShows = false
				} else {
					this.blurProNullShows = true
				}
			},
			// 产品价格失焦
			proNumBlurs(e) {
				if (e) {
					this.blurProNumNullShow = false
				} else {
					this.blurProNumNullShow = true
				}
			},
			// 产品规格失焦
			proSpecsBlurs(e) {
				if (e) {
					this.blurProSpecsNullShow = false
				} else {
					this.blurProSpecsNullShow = true
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				console.log(event.file[0])
				// this.imageUrl = event.file[0].url
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						// status: 'uploading',
						// message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				console.log(url)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseConfig.uploadUrl + '/casugn-system/sys/common/upload',
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							console.log("测试一下上传视频")
							if (data.success) {
								this.imageUrl = data.message
								// this.videoUrl=data.message
								resolve(data.message)
							}
						}
					});
				})
			},
			// 保存
			nextStep() {
				this.imgArr = []
				this.fileList1.forEach(item => {
					this.imgArr.push(item.url)
				})
				if (this.form.name == "") {
					return this.$showToast('产品名称和不能为空')
				} else if (this.form.price == "") {
					return this.$showToast('产品价格不能为空')
				} else if (this.form.specs == "") {
					return this.$showToast('产品规格不能为空')
				} else if (!this.fileList1.length) {
					return this.$showToast('请至少上传一张产品图')
				}
				let data = {
					...this.form,
					supplierId: this.supplierId,
					imageList: this.imgArr.toString(),
					openId: this.openId
				}
				if (this.submitLoad === true) return
				this.submitLoad = true
				let method = this.form.id ? 'supplyProductSaveEdit' : 'supplyProductSave'
				this.$api[method](data).then(res => {
					if (res.success) {
						this.$showToast(this.form.id ? '编辑成功' : '发布成功')
						setTimeout(() => {
							this.submitLoad = false
							uni.navigateBack()
						}, 800)
					} else {
						this.submitLoad = false
						this.$showToast(res.message)
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep {
		.uicon-checkmark {
			font-size: 13px !important;
			top: -4px !important;
		}

		.u-upload__deletable__icon {
			.uicon-close {
				font-size: 11px !important;
				top: 3px !important;
			}
		}

		.u-number-box__minus,
		.u-number-box__input,
		.u-number-box__plus {
			height: 44rpx !important;
		}
	}

	.active-list {
		background: #FFFFFF;
		border-bottom: 1upx solid #f6f6f6;
		padding: 41upx 30upx 0;
		padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
		position: relative;
		height: 100%;

		.active-title {
			letter-spacing: 3upx;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #666666;
			// display: flex;
			// height: 110upx;
			border-bottom: 1px solid #f6f6f6;
			margin-top: 20upx;

			.num-box {
				margin-top: 25rpx;
				height: 38px;
			}

			.active-top {
				font-size: 26upx;
				font-weight: bold;

				.tip {
					margin-left: 8upx;
				}

				text {
					color: red;
					margin-right: 5upx;
				}
			}

			.buy-radio {}

			.buy-many {
				font-size: 22upx;
				color: #2979ff;
				padding-bottom: 10upx;
			}

			::v-deep .u-radio-group {
				margin-top: 20upx;
				margin-bottom: 20upx;
			}

			::v-deep .u-textarea {
				margin-top: 25upx;
				display: flex;
				justify-content: space-between;
				padding: 0;
				// height: 60upx;
				font-size: 30upx;

				textarea {
					width: 200upx !important;
				}
			}

			::v-deep .u-textarea__count {
				position: relative;
			}
		}

		.describe-title {

			// width: 100%;
			::v-deep .u-textarea {
				// width: 200upx !important;
				margin-top: 25upx;
				display: flex;
				display: block;
				justify-content: space-between;
				padding: 0;
				// height: 60upx;
				font-size: 30upx;

				textarea {
					width: 600upx !important;
				}
			}

			::v-deep .u-textarea__count {
				position: relative;
				display: block;
				text-align: right;
				margin-top: 10upx;
			}
		}

		.active-info {
			border-bottom: 1upx solid #EEEEEE;

			// padding: 37upx 0;
			.active-img-des {
				font-size: 24upx;
				color: rgb(153, 153, 153);
				margin-top: 20upx;
			}

			.active-info-img {
				margin: 0 auto;
				// margin-top: 30upx;
				// width: 690upx;
				// height: 350upx;
				border-radius: 30upx;
				position: relative;
				overflow: hidden;
				margin-top: 20upx;

				// border-bottom: 1upx solid #EEEEEE;
				// img {
				// 	width: 690upx;
				// 	height: 350upx;
				// }

				.active-info-mask {
					position: absolute;
					width: 690upx;
					height: 70upx;
					bottom: 0;
					background: linear-gradient(0deg, #000000 0%, rgba(0, 0, 0, 0.1000) 100%);
					line-height: 70upx;
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					text-align: center;
				}
			}
		}


		.sign-up {
			margin-top: 120upx;
		}


	}

	.pos-button {
		width: 100%;
		display: flex;
		justify-content: space-around;
		border-top: 1px solid #e8e8e8;
		margin-top: 20upx;

		.pos-button-cancel {
			width: 50%;
			color: #2684ff;
			height: 100upx;
			line-height: 100upx;
			text-align: center;
		}

		.pos-button-confirm {
			width: 50%;
			background: #2684ff;
			color: white;
			height: 100upx;
			line-height: 100upx;
			text-align: center;
		}
	}
</style>
